package com.cpd.blog.controller;

import com.cpd.blog.common.annotation.Cache;
import com.cpd.blog.common.constant.RedisKey;
import com.cpd.blog.common.group.DeleteGroup;
import com.cpd.blog.dto.Article.ArticleReq;
import com.cpd.blog.dto.Page.PageParams;
import com.cpd.blog.dto.Result;
import com.cpd.blog.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 文章接口
 *
 * @author Copydown
 * @date 2021/8/14 17:03s
 */
@Api(tags = "文章模块")
@RestController
@RequestMapping("articles")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    /**
     * 文章列表
     *
     * @param pageParams 分页参数
     * @return Result
     */
    @ApiOperation("文章列表 接口")
    @PostMapping
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 最热文章
     *
     * @return Result
     */
    @ApiOperation("最热文章 接口")
    @PostMapping("/hot")
    @Cache(expire = 5 * 60 * 1000, name = RedisKey.HOT_ARTICLE)
    public Result hotArticle() {
        return articleService.hotArticle();
    }

    /**
     * 最新文章
     *
     * @return Result
     */
    @ApiOperation("最新文章 接口")
    @PostMapping("/new")
    @Cache(expire = 5 * 60 * 1000, name = RedisKey.NEW_ARTICLE)
    public Result newArticles() {
        return articleService.newArticles();
    }

    /**
     * 获取文章归档列表
     *
     * @return Result
     */
    @ApiOperation("文章归档 接口")
    @PostMapping("/listArchives")
    public Result listArchives() {
        return articleService.listArchives();
    }

    /**
     * 通过 文章Id 查询文章
     *
     * @param articleId 文章Id
     * @return Result
     */
    @ApiOperation("文章详情 接口")
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId) {
        return articleService.findArticleById(articleId);
    }

    /**
     * 文章发表
     *
     * @param articleReq 文章参数
     * @return Result
     */
    @ApiOperation("文章发布 接口")
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleReq articleReq) {
        return articleService.publish(articleReq);
    }


    /**
     * 我的文章
     *
     * @param pageParams pageParams
     * @return Result
     */
    @ApiOperation("我的文章 接口")
    @PostMapping("mine")
    public Result listUserArticles(@RequestBody PageParams pageParams) {
        return articleService.listUserArticle(pageParams);
    }

    /**
     * 修改文章
     *
     * @param articleReq 文章参数
     * @return Result
     */
    @ApiOperation("编辑文章 接口")
    @PostMapping("edit")
    public Result edit(@RequestBody ArticleReq articleReq) {
        return articleService.edit(articleReq);
    }

    /**
     * 根据 文章ID 删除与其相关的所有项
     *
     * @param articleReq articleReq
     * @return Result
     */
    @ApiOperation("删除文章 接口")
    @PostMapping("delete")
    @Validated(DeleteGroup.class)
    public Result delete(@RequestBody ArticleReq articleReq) {
        return articleService.deleteWithComment(articleReq.getId());
    }
}
